﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;

namespace Zeus.Components.ZipTool.Ionic
{
    public class ZipHelper
    {
        public static bool ZipCompare(string srcZipPath, string dstZipPath)
        {
            List<ZipEntryInfo> srcZipFileList = GetZipFileList(srcZipPath);

            List<ZipEntryInfo> dstZipFileList = GetZipFileList(dstZipPath);

            return CompareFileList(srcZipFileList, dstZipFileList);
        }

        public static List<ZipEntryInfo> GetZipFileList(string zipFile)
        {
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile)))
            {
                ZipEntry theEntry;
                List<ZipEntryInfo> zipFileList = new List<ZipEntryInfo>();

                while ((theEntry = s.GetNextEntry()) != null)
                {
                    var fileInfo = new ZipEntryInfo
                    {
                        FileName = theEntry.FileName,
                        Crc = theEntry.Crc
                    };

                    zipFileList.Add(fileInfo);
                }

                return zipFileList;
            }
        }

        public static bool CompareFileList(List<ZipEntryInfo> srcZipFileList, List<ZipEntryInfo> dstZipFileList)
        {
            if (srcZipFileList.Count != dstZipFileList.Count)
            {
                return false;
            }

            return srcZipFileList.All(fileInfo => dstZipFileList.Contains(fileInfo));
        }
    }
}
